/**
 * 
 */
package util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * @author Kristin Behrens, Robin Gawenda, Swen Jung
 *
 */
public class SessionListener implements HttpSessionListener {

	private static List<Long> users = Collections.synchronizedList(
            new ArrayList<Long>());
	
	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
	 */
	@Override
	public void sessionCreated(HttpSessionEvent arg0) {
		users.add((Long)arg0.getSession().getAttribute(Constant.USERID));
		System.out.println("Neuer Benutzer. Anzahl aktiver Benutzer: " +users.size());
	}

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
	 */
	@Override
	public void sessionDestroyed(HttpSessionEvent arg0) {
		users.remove(arg0.getSession().getAttribute(Constant.USERID));
		System.out.println("Benutzer weniger. Anzahl aktiver Benutzer: " +users.size());
	}
	
	public static int getUsersOnline() {
		return users.size();
	}
	
	public static boolean isUserOnline(Long user_id) {
		return users.contains(user_id);
	}

}
